package com.pactera.madp.cp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.wxGZH.KeywordAddDTO;
import com.pactera.madp.cp.api.dto.wxGZH.KeywordUpdateDTO;
import com.pactera.madp.cp.api.entity.wxgzh.KeyWordEntity;
import com.pactera.madp.cp.api.page.SubPage;
import com.pactera.madp.cp.api.vo.wxgzh.QueryKeywordVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.wxgzh.WxGzhKeywordMapper;
import com.pactera.madp.cp.service.WxGzhKeywordService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxGzhKeywordServiceImpl extends ServiceImpl<WxGzhKeywordMapper, KeyWordEntity> implements WxGzhKeywordService {

    @Override
    public List<String> getMsgByKeyword(String content, Integer corpId) {
        List<String> employeeIds = this.baseMapper.getMsgByKeyword(content, corpId);
        return employeeIds;
    }

    @Override
    public Boolean saveKeyword(KeywordAddDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        //查询问题描述
        KeyWordEntity problemEntity = getOne(Wrappers.<KeyWordEntity>lambdaQuery()
                .eq(KeyWordEntity::getCorpId, corpId)
                .eq(KeyWordEntity::getProblemDescription, dto.getProblemDescription()));
        if(problemEntity != null){
            throw new CheckedException("问题描述不能重复");
        }
        KeyWordEntity entity = new KeyWordEntity();
        entity.setProblemDescription(dto.getProblemDescription());
        String[] split = dto.getKeyword().split(",");
        String s = JSONObject.toJSONString(split);
        entity.setKeyword(s);
        entity.setReMsg(dto.getReMsg());
        entity.setCorpId(CorpContextHolder.getCorpId());
        return save(entity);
    }

    @Override
    public Boolean updateKeyword(KeywordUpdateDTO dto) {
        KeyWordEntity entity = new KeyWordEntity();
        entity.setId(dto.getId());
        Integer corpId = CorpContextHolder.getCorpId();
        KeyWordEntity problemEntity = getOne(Wrappers.<KeyWordEntity>lambdaQuery()
                .eq(KeyWordEntity::getCorpId, corpId)
                .eq(KeyWordEntity::getId,dto.getId())
                .eq(KeyWordEntity::getProblemDescription, dto.getProblemDescription()));
        if(problemEntity != null){
            throw new CheckedException("问题描述不能重复");
        }
        entity.setProblemDescription(dto.getProblemDescription());
        String[] split = dto.getKeyword().split(",");
        String s = JSONObject.toJSONString(split);
        entity.setKeyword(s);
        entity.setReMsg(dto.getReMsg());
        entity.setCorpId(CorpContextHolder.getCorpId());
        return update(entity,Wrappers.<KeyWordEntity>lambdaQuery().eq(KeyWordEntity::getId,dto.getId()));
    }

    @Override
    public Boolean deleteKeyword(Integer id) {
        return removeById(id);
    }

    @Override
    public R<Page<List<QueryKeywordVO>>> pageQuery(Page page, String problem) {
        LambdaQueryChainWrapper<KeyWordEntity> wrapper = lambdaQuery().eq(KeyWordEntity::getCorpId,CorpContextHolder.getCorpId());
        wrapper.orderByDesc(KeyWordEntity::getId);
        if(!StringUtils.isEmpty(problem))
            wrapper.like(KeyWordEntity::getProblemDescription,problem);
        IPage page1 = wrapper.page(page);
        List<QueryKeywordVO> vos = new ArrayList<>();
        List<KeyWordEntity> EntityList = page1.getRecords();
        for(KeyWordEntity entity:EntityList){
            QueryKeywordVO vo = new QueryKeywordVO();
            vo.setId(entity.getId());
            vo.setProblemDescription(entity.getProblemDescription());
            List<String> strings = JSONObject.parseArray(entity.getKeyword(), String.class);
            StringBuilder s = new StringBuilder();
            strings.forEach(ss->{
                s.append(ss);
                s.append(",");
            });
            vo.setKeyword(s.substring(0,s.length()-1));
            vo.setReMsg(entity.getReMsg());
            vos.add(vo);
        }
        SubPage subPage = new SubPage();
        subPage.setRecords(vos);
        subPage.setCurrent(page.getCurrent());
        subPage.setSize(page.getSize());
        subPage.setTotal(page.getTotal());
        return R.ok(subPage);
    }
}
